#pragma once

#ifndef WEBTABWIDGET_H
#define WEBTABWIDGET_H

#include <QtCore>
#include <QtGui>
#include <QtWidgets>

#include "webwnd.h"
#include "webtabbutton.h"

class WebTabWidget : public QTabWidget
{
    Q_OBJECT
public:
    explicit WebTabWidget(QWidget *parent = nullptr);
    ~WebTabWidget();

public:
    WebWnd * webWnd(int index) { return qobject_cast<WebWnd*>(this->widget(index)); }
    WebWnd * curWebWnd() { return qobject_cast<WebWnd*>(this->currentWidget()); }

public:    
    WebWnd * newTab(const QString & strUrl = "", bool current = true, int index = -1);

    int addTab(WebWnd *wnd, bool current = true, int index = -1);
    WebWnd * cloneTab(int index);
    int closeTab(int index);
    int closeOtherTab(int index);
    int closeAllTab();
    int reloadTab(int index);
    int reloadOtherTab(int index);
    int reloadAllTab();

    QWidget* getButtonWidget();
    QWidget* getNewWidget();
    QWidget* getTitleWidget();
    QWidget* getMenuWidget();
    QWidget* getMinWidget();
    QWidget* getMaxWidget();
    QWidget* getCloseWidget();

protected:
    WebTabButton *  m_tabButton;
    QMenu *         m_menuTab;
    QMenu *         m_menuBlank;
    int             m_hitIndex;
    QIcon           m_tabIcon;
    int             m_lastIndex;

protected:
    void init();
    void initMenu();

    QWidget * topParentWidget();

protected slots:
    void onNewTab();
    void onCloneTab();

    void onCloseTab();
    void onCloseOtherTab();
    void onCloseAllTab();

    void onReloadTab();
    void onReloadOtherTab();
    void onReloadAllTab();

    void onTabContextMenuRequested(const QPoint & pos);
    void onTabCloseRequested(int index);
    void onTabDoubleClicked(int index);
    void onCurrentChanged(int index);

    void onWebIconChanged(const QIcon &icon);
    void onWebTitleChanged(const QString &title);

    void onNewWebWnd(QString strUrl, bool current);

signals:
    void btNewClicked();
    void btMenuClicked();
    void btMinClicked();
    void btMaxClicked();
    void btCloseClicked();
    void allTabClosed();

    void newMainWnd(QString strUrl);
};

#endif // WEBTABWIDGET_H
